package demo.swing;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ComboBoxModel;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListDataListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.AbstractTableModel;

import demo.swing.menu.DemoMenu;
import demo.swing.passenger.EnterPassenger;
import demo.swing.passenger.EnterPassengerListener;

/**
 * 
 * @author MAbernethy
 */
public class FlightReservation extends JFrame implements ActionListener, ListSelectionListener
{

	private javax.swing.JPanel jContentPane = null;

	private javax.swing.JLabel lblDepart = null;
	private javax.swing.JLabel lblDest = null;
	private javax.swing.JButton btnSearch = null;
	private javax.swing.JTable tblFlights = null;
	private javax.swing.JScrollPane jScrollPane = null;
//	private javax.swing.JTextField txtNumTixx = null;
//	private javax.swing.JLabel lblNumTixx = null;
	private javax.swing.JButton btnPurchase = null;
	private javax.swing.JComboBox comboDepart = null;
	private javax.swing.JComboBox comboDest = null;
	
	public FlightTableModel flightModel;
	private CityComboModel comboModel1;
	private CityComboModel comboModel2;
	
	public FlightReservation() throws Exception
	{
		super();
		initialize();
		
/*		getTxtNumTixx().setEnabled(false);
		getLblNumTixx().setEnabled(false);
*/		getBtnPurchase().setEnabled(false);
		
		flightModel = new FlightTableModel();
		getTblFlights().setModel(flightModel);
		SqlQuery q = new SqlQuery("192.168.1.4", "5432", new String("pgadmin"), new String("Vyrdalak"));
		ResultSet res = q.executeSqlQuery("SELECT DISTINCT \"City\" FROM public.airport");
		if (res != null){
			List l = new ArrayList();
			while(res.next()){
				l.add(res.getString(1));
			}
			comboModel1 = new CityComboModel(l);
			comboModel2 = new CityComboModel(l);
			getComboDepart().setModel(comboModel1);
			getComboDest().setModel(comboModel2);
		}
		q.close();		
		
	}

	private void initialize()
	{
		this.setSize(560, 330);
		this.setContentPane(getJContentPane());
		this.setTitle("Flight Reservation");
	}
	
	public void actionPerformed(ActionEvent e)
	{
		if (e.getSource() == getBtnSearch())
		{
			String dest = getComboDest().getSelectedItem().toString();
			String depart = getComboDepart().getSelectedItem().toString();
			List l = DataHandler.searchRecords(depart, dest);
			flightModel.updateData(l);
		}
		else if (e.getSource() == getBtnPurchase())
		{
			Object o = flightModel.getData().get(getTblFlights().getSelectedRow());
			EnterPassenger enterPassenger = new EnterPassenger("Enter private information!");
			enterPassenger.pack();
			enterPassenger.setVisible(true);
			enterPassenger.getOkBtn().addActionListener(new EnterPassengerListener(enterPassenger, (Flight) o));
//			int tixx = Integer.parseInt(getTxtNumTixx().getText());
//			try
//			{
//				DataHandler.updateRecords(o, tixx);
//			}
//			catch (Exception ex)
//			{
//				JOptionPane.showConfirmDialog(this, ex.getMessage(), "Error", JOptionPane.OK_CANCEL_OPTION, JOptionPane.ERROR_MESSAGE);
//			}
		}
	}
	
	public void valueChanged(ListSelectionEvent e)
	{
		boolean selected = getTblFlights().getSelectedRow() > -1;
		getBtnPurchase().setEnabled(selected);
	}
	
	public class FlightTableModel extends AbstractTableModel
	{
		private List data = new ArrayList();
		
		public void updateData(List l)
		{
			data = l;
			fireTableDataChanged();
		}	
		
		public List getData()
		{
			return data;	
		}
		
		public int getRowCount()
		{
			return data.size();
		}

		public int getColumnCount()
		{
			return 7;
		}
		
		public String getColumnName(int col)
		{
			switch (col) {
			case 0:
				return "Departure";
			case 1:
				return "Destination";				
			case 2:
				return "Flight Number";
			case 3:
				return "Date";				
			case 4:
				return "Number of tickets\n in Bisnes class";
			case 5:
				return "Number of tickets\n in Econom class";
			case 6:
				return "Time of departure";
			default:
				return "Error";
			}
		}

		public Object getValueAt(int row, int col)
		{
			if (col == 0)
				return ((Flight)(data.get(row))).getDeparture();
			else if (col == 1)
				return ((Flight)(data.get(row))).getDestination();
			else if (col == 2)
				return new String(((Flight)(data.get(row))).getFlightNum());
			else if (col == 3)
				return new String(((Flight)(data.get(row))).getDate());
			else if (col == 4)
				return new String(((Flight)(data.get(row))).getTicketsOnBisnesClass());
			else if (col == 5)
				return new String(((Flight)(data.get(row))).getTicketsOnEconomClass());
			else
				return new String(((Flight)(data.get(row))).getTimeOfDeparture());
		}
		
	}
	
	private class CityComboModel implements ComboBoxModel
	{
		private List data = new ArrayList();
		private int selected = 0;

		public CityComboModel(List list)
		{
			data = list;
		}

		public void setSelectedItem(Object o)
		{
			selected = data.indexOf(o);
		}

		public Object getSelectedItem()
		{
			return data.get(selected);
		}

		public int getSize()
		{
			return data.size();
		}

		public Object getElementAt(int i)
		{
			return data.get(i);
		}

		public void addListDataListener(ListDataListener arg0)
		{
		}

		public void removeListDataListener(ListDataListener arg0)
		{
		}
	}

	public static void main(String[] args)
	{
		FlightReservation f;
		try {
			f = new FlightReservation();
			f.setVisible(true);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}



	private javax.swing.JPanel getJContentPane()
	{
		if (jContentPane == null)
		{
			jContentPane = new javax.swing.JPanel();
			this.setJMenuBar(new DemoMenu());
			java.awt.GridBagConstraints consGridBagConstraints2 = new java.awt.GridBagConstraints();
			java.awt.GridBagConstraints consGridBagConstraints3 = new java.awt.GridBagConstraints();
			java.awt.GridBagConstraints consGridBagConstraints1 = new java.awt.GridBagConstraints();
			java.awt.GridBagConstraints consGridBagConstraints4 = new java.awt.GridBagConstraints();
			java.awt.GridBagConstraints consGridBagConstraints6 = new java.awt.GridBagConstraints();
			java.awt.GridBagConstraints consGridBagConstraints7 = new java.awt.GridBagConstraints();
			java.awt.GridBagConstraints consGridBagConstraints5 = new java.awt.GridBagConstraints();
			java.awt.GridBagConstraints consGridBagConstraints8 = new java.awt.GridBagConstraints();
			java.awt.GridBagConstraints consGridBagConstraints9 = new java.awt.GridBagConstraints();
			consGridBagConstraints8.insets = new java.awt.Insets(32,6,8,18);
			consGridBagConstraints8.ipadx = 68;
			consGridBagConstraints8.fill = java.awt.GridBagConstraints.HORIZONTAL;
			consGridBagConstraints8.weightx = 1.0;
			consGridBagConstraints8.gridwidth = 2;
			consGridBagConstraints8.gridy = 0;
			consGridBagConstraints8.gridx = 1;
			consGridBagConstraints2.insets = new java.awt.Insets(33,3,10,5);
			consGridBagConstraints2.ipady = 6;
			consGridBagConstraints2.ipadx = 27;
			consGridBagConstraints2.gridy = 0;
			consGridBagConstraints2.gridx = 3;
			consGridBagConstraints6.insets = new java.awt.Insets(11,17,15,4);
			consGridBagConstraints6.ipady = 5;
			consGridBagConstraints6.ipadx = 36;
			consGridBagConstraints6.gridwidth = 2;
			consGridBagConstraints6.gridy = 2;
			consGridBagConstraints6.gridx = 0;
			consGridBagConstraints4.insets = new java.awt.Insets(7,15,4,18);
			consGridBagConstraints4.ipady = -246;
			consGridBagConstraints4.ipadx = 66;
			consGridBagConstraints4.fill = java.awt.GridBagConstraints.BOTH;
			consGridBagConstraints4.weighty = 1.0;
			consGridBagConstraints4.weightx = 1.0;
			consGridBagConstraints4.gridwidth = 6;
			consGridBagConstraints4.gridy = 1;
			consGridBagConstraints4.gridx = 0;
			consGridBagConstraints7.insets = new java.awt.Insets(5,96,10,18);
			consGridBagConstraints7.ipady = 6;
			consGridBagConstraints7.ipadx = 10;
			consGridBagConstraints7.gridwidth = 2;
			consGridBagConstraints7.gridy = 2;
			consGridBagConstraints7.gridx = 4;
			consGridBagConstraints3.insets = new java.awt.Insets(30,8,6,18);
			consGridBagConstraints3.ipady = 3;
			consGridBagConstraints3.gridy = 0;
			consGridBagConstraints3.gridx = 5;
			consGridBagConstraints1.insets = new java.awt.Insets(33,14,10,6);
			consGridBagConstraints1.ipady = 6;
			consGridBagConstraints1.ipadx = 34;
			consGridBagConstraints1.gridy = 0;
			consGridBagConstraints1.gridx = 0;
			consGridBagConstraints5.insets = new java.awt.Insets(11,5,16,2);
			consGridBagConstraints5.ipadx = 62;
			consGridBagConstraints5.fill = java.awt.GridBagConstraints.HORIZONTAL;
			consGridBagConstraints5.weightx = 1.0;
			consGridBagConstraints5.gridy = 2;
			consGridBagConstraints5.gridx = 2;
			consGridBagConstraints9.insets = new java.awt.Insets(32,5,8,8);
			consGridBagConstraints9.ipadx = 68;
			consGridBagConstraints9.fill = java.awt.GridBagConstraints.HORIZONTAL;
			consGridBagConstraints9.weightx = 1.0;
			consGridBagConstraints9.gridy = 0;
			consGridBagConstraints9.gridx = 4;
			jContentPane.setLayout(new java.awt.GridBagLayout());
			jContentPane.add(getLblDepart(), consGridBagConstraints1);
			jContentPane.add(getLblDest(), consGridBagConstraints2);
			jContentPane.add(getBtnSearch(), consGridBagConstraints3);
			jContentPane.add(getJScrollPane(), consGridBagConstraints4);
//			jContentPane.add(getTxtNumTixx(), consGridBagConstraints5);
//			jContentPane.add(getLblNumTixx(), consGridBagConstraints6);
			jContentPane.add(getBtnPurchase(), consGridBagConstraints7);
			jContentPane.add(getComboDepart(), consGridBagConstraints8);
			jContentPane.add(getComboDest(), consGridBagConstraints9);
		}
		return jContentPane;
	}

	public javax.swing.JLabel getLblDepart() {
		if(lblDepart == null) {
			lblDepart = new javax.swing.JLabel();
			lblDepart.setText("Departure:");
			lblDepart.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
		}
		return lblDepart;
	}

	public javax.swing.JLabel getLblDest() {
		if(lblDest == null) {
			lblDest = new javax.swing.JLabel();
			lblDest.setText("Destination:");
			lblDest.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
		}
		return lblDest;
	}

	public javax.swing.JButton getBtnSearch() {
		if(btnSearch == null) {
			btnSearch = new javax.swing.JButton();
			btnSearch.setText("Search");
			btnSearch.setMnemonic(java.awt.event.KeyEvent.VK_S);
			btnSearch.addActionListener(new PostgresqGetFlightslHandler(this));
		}
		return btnSearch;
	}

	public javax.swing.JTable getTblFlights() {
		if(tblFlights == null) {
			tblFlights = new javax.swing.JTable();
			tblFlights.getSelectionModel().addListSelectionListener(this);
			tblFlights.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		}
		return tblFlights;
	}

	public javax.swing.JScrollPane getJScrollPane() {
		if(jScrollPane == null) {
			jScrollPane = new javax.swing.JScrollPane();
			jScrollPane.setViewportView(getTblFlights());
		}
		return jScrollPane;
	}

//	public javax.swing.JTextField getTxtNumTixx() {
//		if(txtNumTixx == null) {
//			txtNumTixx = new javax.swing.JTextField();
//		}
//		return txtNumTixx;
//	}
//
//	public javax.swing.JLabel getLblNumTixx() {
//		if(lblNumTixx == null) {
//			lblNumTixx = new javax.swing.JLabel();
//			lblNumTixx.setText("Number of Tickets:");
//			lblNumTixx.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
//		}
//		return lblNumTixx;
//	}

	public javax.swing.JButton getBtnPurchase() {
		if(btnPurchase == null) {
			btnPurchase = new javax.swing.JButton();
			btnPurchase.setText("Purchase");
			btnPurchase.addActionListener(this);
		}
		return btnPurchase;
	}

	public javax.swing.JComboBox getComboDepart() {
		if(comboDepart == null) {
			comboDepart = new javax.swing.JComboBox();
		}
		return comboDepart;
	}

	public javax.swing.JComboBox getComboDest() {
		if(comboDest == null) {
			comboDest = new javax.swing.JComboBox();
		}
		return comboDest;
	}


}  //  @jve:visual-info  decl-index=0 visual-constraint="10,10"
